package com.budwk.app.util.poiUtils;

import cn.hutool.core.date.DateUtil;
import com.budwk.app.sqfw.dto.FireHiddenCheckWordDTO;
import com.budwk.app.sqfw.dto.RefuseClassifyExcelParamDTO;
import com.budwk.app.sqfw.enums.DownLoadFileNameEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary;
import com.budwk.app.sqfw.vo.GenerateWordAndPdfUrlVO;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.RefuseExcelPictureBO;
import com.deepoove.poi.data.PictureRenderData;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

import static com.budwk.app.util.poiUtils.CommonFormOfficeUtils.*;

/**
 * @author ZY.Wang
 * @since 2023/7/24
 */
public class RefuseClassifyOfficeUtils {

    public static GenerateWordAndPdfUrlVO createWordAndPdfUrlCheckForm(FireHiddenCheckWordDTO formSummary) {
        long startTime = System.currentTimeMillis();

        Map<String, Object> paramMap = Maps.newHashMap();

        String unitName = formSummary.getCompleteUnitName();

        paramMap.put("unitName", unitName);

        List<String> noSigCreateUserList = Lists.newArrayList();
        List<String> sigCreateUserList = Lists.newArrayList();
        if (StringUtils.isBlank(formSummary.getRefuseClassifyCreateUserSigUrl()))
            noSigCreateUserList.add(formSummary.getCreateUserName());
        else sigCreateUserList.add(formSummary.getRefuseClassifyCreateUserSigUrl());
        addWordUserSignature(paramMap, "cuNoSig", "cuSigs", "cuSig", noSigCreateUserList, sigCreateUserList, 1.0F);
        paramMap.put("createUserName", formSummary.getCreateUserName());

        paramMap.put("createDateStr", DateUtil.format(formSummary.getCreateDate(), "yyyy-MM-dd"));
        paramMap.put("a3", formSummary.getA3());
        paramMap.put("a20", formSummary.getA20());
        paramMap.put("a17", formSummary.getA17());

        String pictureUrlStr = formSummary.getA9();
        List<String> pictureUrlList = Lists.newArrayList(pictureUrlStr.split(","));
        List<Map<String, PictureRenderData>> pictures = CommonFormOfficeUtils.addWordPicture(pictureUrlList, "a9", 11F);
        paramMap.put("a9s", pictures);

        Date fillDate = formSummary.getFillDate();
        if (fillDate == null) fillDate = formSummary.getCreateDate();
        String fillDateStr = DateUtil.format(fillDate, "yyyy年MM月dd日");
        paramMap.put("fillDateStr", fillDateStr);

        //String fileName = unitName + "垃圾分类巡查表-" + formSummary.getCreateUserName() + DateUtil.format(new Date(), "yyyyMMddHHmmss");
        //zhangp 2023-12-13 文集名统一
        String downloadFileName = DownLoadFileNameEnum.RUBBISH_CLASS_PDF.getName();
        String fileName = String.format("%s%s-%s%s", unitName, downloadFileName, formSummary.getCreateUserName(), DateUtil.format(new Date(), "yyyyMMddHHmmss"));

        return CommonFormOfficeUtils.generateWordPdfAndUpload(paramMap, "refuseClassify.docx", fileName, "垃圾分类", startTime);
    }

    public static String createRefuseClassifyExcel(RefuseClassifyExcelParamDTO excelParamDTO, String isCheckData) {

        if (excelParamDTO.getCompleteUnitName().contains("云潭街道")) {
            return createRefuseClassifyExcelYuHongSQ(excelParamDTO, isCheckData);
        }

        Map<String, Object> excelParam = new HashMap<>();
        excelParam.put("unitName", excelParamDTO.getCompleteUnitName());
        excelParam.put("fillUnitName", excelParamDTO.getCompleteUnitName());
        excelParam.put("downloadDate", DateUtil.format(new Date(), "yyyy 年 MM 月 dd 日"));

        List<Map<String, Object>> listMap = new ArrayList<>();
        List<Sqfw_formSummary> formSummaryList = excelParamDTO.getFormSummaryList();
        for (int i = 0; i < formSummaryList.size(); i++) {
            Sqfw_formSummary formSummary = formSummaryList.get(i);
            Map<String, Object> lm = new HashMap<>();
            lm.put("num", i + 1 + "");

            Date fillDate = formSummary.getFillDate();
            if (fillDate == null) fillDate = formSummary.getCreateDate();
            String fillDateStr = DateUtil.format(fillDate, "yyyy.MM.dd HH:mm");
            lm.put("fillDateStr", fillDateStr);

            lm.put("a3", formSummary.getA3());

            lm.put("a20", formSummary.getA20());

            lm.put("a17", formSummary.getA17());

            String a9 = formSummary.getA9();
            //zhangp 2023-11-27 改下载时报空指针异常
            List<byte[]> a9ByteList = Lists.newArrayList();
            if (StringUtils.isNotBlank(a9)) {
                List<String> a9UrlStrList = Lists.newArrayList(a9.split(","));
                if (a9UrlStrList.size() > 2) a9UrlStrList = a9UrlStrList.subList(0, 2);
                a9ByteList = addPictureToByteList(a9UrlStrList);

            }

            lm.put("a9", a9ByteList);
            listMap.add(lm);
            // zhangp 2023-12-07 检查数据时只要有一条就返回
            if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                return "true";
            }
        }
        if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
            if (listMap.isEmpty()) {
                return "false";
            }
        }
        excelParam.put("list", listMap);

        String path = getTemplatePathByOS("refuseClassifyRec.xlsx");

        //String fileName = "Excel垃圾分类清单";
        String fileName = DownLoadFileNameEnum.RUBBISH_CLASS_LIST.getName();

        if (StringUtils.isNotBlank(excelParamDTO.getFileNameSuffix()))
            fileName = fileName + excelParamDTO.getFileNameSuffix();

        ExcelFileCreateParamBO excelFileCreateParam = new ExcelFileCreateParamBO();
        excelFileCreateParam.setTemplatePath(path);
        excelFileCreateParam.setSheetParam(excelParam);
        excelFileCreateParam.setDataList(listMap);
        excelFileCreateParam.setClazz(RefuseExcelPictureBO.class);
        excelFileCreateParam.setFileName(fileName);
        excelFileCreateParam.setReturnWebUrlFlag(excelParamDTO.getReturnWebUrlFlag());

        return createExcelAndUpload(excelFileCreateParam);
    }

    /**
     * 创建宇虹社区垃圾分类pdf
     *
     * @param formSummary 工作文档
     * @return 结果
     */
    public static GenerateWordAndPdfUrlVO createWordAndPdfUrlCheckFormYuHongSQ(FireHiddenCheckWordDTO formSummary) {
        long startTime = System.currentTimeMillis();

        Map<String, Object> paramMap = Maps.newHashMap();

        String unitName = formSummary.getCompleteUnitName();

        paramMap.put("unitName", unitName);

        List<String> noSigCreateUserList = Lists.newArrayList();
        List<String> sigCreateUserList = Lists.newArrayList();
        if (StringUtils.isBlank(formSummary.getRefuseClassifyCreateUserSigUrl()))
            noSigCreateUserList.add(formSummary.getCreateUserName());
        else sigCreateUserList.add(formSummary.getRefuseClassifyCreateUserSigUrl());
        addWordUserSignature(paramMap, "cuNoSig", "cuSigs", "cuSig", noSigCreateUserList, sigCreateUserList, 1.0F);
        paramMap.put("createUserName", formSummary.getCreateUserName());

        Date fillDate = formSummary.getFillDate();
        if (fillDate == null) fillDate = formSummary.getCreateDate();
        String fillDateStr = DateUtil.format(fillDate, "yyyy年MM月dd日");
        paramMap.put("fillDateStr", fillDateStr);

        paramMap.put("a3", formSummary.getA3());
        paramMap.put("a20", formSummary.getA20());

        String pictureUrlStr = formSummary.getA9();
        List<Map<String, PictureRenderData>> pictures = Lists.newArrayList();
        //zhangp 2023-11-28 改 判断该字段是否为空
        if (StringUtils.isNotBlank(pictureUrlStr)) {
            List<String> pictureUrlList = Lists.newArrayList(pictureUrlStr.split(","));
            pictures = CommonFormOfficeUtils.addWordPicture(pictureUrlList, "a9", 11F);

        }
        paramMap.put("a9s", pictures);

        paramMap.put("a18", formSummary.getA18());

        paramMap.put("a17", formSummary.getA17());

        //String fileName = unitName + "垃圾分类巡查表-" + formSummary.getCreateUserName() + DateUtil.format(new Date(), "yyyyMMddHHmmss");
        //zhangp 2023-12-13 文集名统一
        String downloadFileName = DownLoadFileNameEnum.RUBBISH_CLASS_PDF.getName();
        String fileName = String.format("%s%s-%s%s", unitName, downloadFileName, formSummary.getCreateUserName(), DateUtil.format(new Date(), "yyyyMMddHHmmss"));

        return CommonFormOfficeUtils.generateWordPdfAndUpload(paramMap, "refuseClassifyYuHongSQ.docx", fileName, "垃圾分类", startTime);
    }

    public static String createRefuseClassifyExcelYuHongSQ(RefuseClassifyExcelParamDTO excelParamDTO, String isCheckData) {
        Map<String, Object> excelParam = new HashMap<>();
        excelParam.put("areaLevel", excelParamDTO.getAreaLevel());

        List<Map<String, Object>> listMap = new ArrayList<>();
        List<Sqfw_formSummary> formSummaryList = excelParamDTO.getFormSummaryList();
        for (int i = 0; i < formSummaryList.size(); i++) {
            Sqfw_formSummary formSummary = formSummaryList.get(i);
            Map<String, Object> lm = new HashMap<>();
            lm.put("num", i + 1 + "");

            Date fillDate = formSummary.getFillDate();
            if (fillDate == null) fillDate = formSummary.getCreateDate();
            String fillDateStr = DateUtil.format(fillDate, "yyyy.MM.dd HH:mm");
            lm.put("fillDateStr", fillDateStr);

            lm.put("a20", formSummary.getA20());

            lm.put("a3", formSummary.getA3());

            String a9 = formSummary.getA9();
            //zhangp 2023-11-27 改下载时报空指针异常
            List<byte[]> a9ByteList = Lists.newArrayList();
            if (StringUtils.isNotBlank(a9)) {
                List<String> a9UrlStrList = Lists.newArrayList(a9.split(","));
                if (a9UrlStrList.size() > 2) a9UrlStrList = a9UrlStrList.subList(0, 2);
                a9ByteList = addPictureToByteList(a9UrlStrList);

            }
            lm.put("a9", a9ByteList);

            lm.put("a18", formSummary.getA18());

            lm.put("a17", formSummary.getA17());
            listMap.add(lm);
            if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                return "true";
            }
        }
        if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
            if (listMap.isEmpty()) {
                return "false";
            }
        }
        excelParam.put("list", listMap);

        String path = getTemplatePathByOS("refuseClassifyYuHongSQ.xlsx");

        //String fileName = "Excel垃圾分类清单";
        String fileName = DownLoadFileNameEnum.RUBBISH_CLASS_LIST.getName();
        if (StringUtils.isNotBlank(excelParamDTO.getFileNameSuffix()))
            fileName = fileName + excelParamDTO.getFileNameSuffix();

        ExcelFileCreateParamBO excelFileCreateParam = new ExcelFileCreateParamBO();
        excelFileCreateParam.setTemplatePath(path);
        excelFileCreateParam.setSheetParam(excelParam);
        excelFileCreateParam.setDataList(listMap);
        excelFileCreateParam.setClazz(RefuseExcelPictureBO.class);
        excelFileCreateParam.setFileName(fileName);
        excelFileCreateParam.setReturnWebUrlFlag(excelParamDTO.getReturnWebUrlFlag());

        return createExcelAndUpload(excelFileCreateParam);
    }
}
